package com.junzhiit.nengji.shop.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bainaeco.framework.exception.InvalidArgumentException;
import com.bainaeco.framework.util.AssertUtil;
import com.junzhiit.nengji.bo.ShopClientBO;
import com.junzhiit.nengji.shop.service.MemberCardService;
import com.junzhiit.nengji.shop.web.vo.PurchaseMemberCardVO;
import com.junzhiit.nengji.shop.web.vo.RechargeMemberCardVO;
import com.junzhiit.nengji.shop.web.vo.ResultVO;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/member/card")
public class MemberCardController extends BaseController{
	
	@Autowired
	private MemberCardService memberCardService;
	
	@ApiOperation(value = "查询用户所拥有的会员卡信息")
	@RequestMapping(value = "/all",method = RequestMethod.GET)
	public ResultVO findAll(ShopClientBO shopClientBO){
		AssertUtil.notBlank(shopClientBO.getUserId(), "userId is required.");
		AssertUtil.notBlank(shopClientBO.getCsId(), "csId is required.");
		return memberCardService.findAll(shopClientBO);
	}
	
	@ApiOperation(value = "根据csId取得店铺的可领取(购买)的会员卡信息")
	@RequestMapping(value = "/shop/{csId}",method = RequestMethod.GET)
	public ResultVO getShopMemberCard(@PathVariable String csId){
		AssertUtil.notBlank(csId, "csId is required.");
		return memberCardService.getShopMemberCard(csId);
	}
	
	@ApiOperation(value = "根据店铺Id数组查询用户对应的会员卡")
	@RequestMapping(value = "/shop/card",method = RequestMethod.GET)
	public ResultVO findAllByShopIds(@RequestParam("csIds")List<String> csIds,String userId){
		AssertUtil.notBlank(userId, "userId is required.");
		if(csIds==null || csIds.size()<=0){
			throw new InvalidArgumentException("csIds is required.");
		}
		return memberCardService.findAllByShopIds(csIds,userId);
	}
	
	@ApiOperation(value = "用户领取会员卡(无门槛普通领取)")
	@RequestMapping(value = "/receive",method = RequestMethod.POST)
	public ResultVO receive(String userId,String csId,String memberCardId){
		AssertUtil.notBlank(userId, "userId is required.");
		AssertUtil.notBlank(csId, "csId is required.");
		AssertUtil.notBlank(memberCardId, "memberCardId is required.");
		return memberCardService.receive(userId,csId,memberCardId);
	}
	
	@ApiOperation(value = "购买会员卡")
	@RequestMapping(value = "/purchase",method = RequestMethod.POST)
	public ResultVO purchaseMemberCard(HttpServletRequest request,PurchaseMemberCardVO purchaseMemberCardVO){
		AssertUtil.notBlank(purchaseMemberCardVO.getUserId(), "userId is required.");
		AssertUtil.notBlank(purchaseMemberCardVO.getCsId(), "csId is required.");
		AssertUtil.notBlank(purchaseMemberCardVO.getMemberCardId(), "memberCardId is required.");
		AssertUtil.notBlank(purchaseMemberCardVO.getTradeType(), "tradeType is required.");
		AssertUtil.notBlank(purchaseMemberCardVO.getWcId(), "wcId is required.");
		return memberCardService.purchaseMemberCard(request,purchaseMemberCardVO);
	}
	
	@ApiOperation(value = "充值会员卡")
	@RequestMapping(value = "/recharge",method = RequestMethod.POST)
	public ResultVO rechargeMemberCard(HttpServletRequest request,RechargeMemberCardVO cardVO){
		AssertUtil.notBlank(cardVO.getUserId(), "userId is required.");
		AssertUtil.notBlank(cardVO.getCsId(), "csId is required.");
		AssertUtil.notNull(cardVO.getMoney(), "money is required.");
		AssertUtil.notBlank(cardVO.getTradeType(), "tradeType is required.");
		AssertUtil.notBlank(cardVO.getWcId(), "wcId is required.");
		return memberCardService.rechargeMemberCard(request,cardVO);
	}
	
	@ApiOperation(value = "会员卡权益详情")
	@RequestMapping(value = "/details",method = RequestMethod.GET)
	public ResultVO details(String memberCardId){
		AssertUtil.notBlank(memberCardId, "memberCardId is required.");
		return memberCardService.details(memberCardId);
	}
	
	@ApiOperation(value = "会员卡充值配置")
	@RequestMapping(value = "/charge/rule",method = RequestMethod.GET)
	public ResultVO chargeRule(String csId,String userId){
		AssertUtil.notBlank(csId, "csId is required.");
		AssertUtil.notBlank(userId, "userId is required.");
		return memberCardService.chargeRule(csId,userId);
	}
	
	@ApiOperation(value = "会员账户信息")
	@RequestMapping(value = "/shop/client",method = RequestMethod.GET)
	public ResultVO shopClient(String csId,String userId){
		AssertUtil.notBlank(csId, "csId is required.");
		AssertUtil.notBlank(userId, "userId is required.");
		return memberCardService.findClientByUserId(csId, userId);
	}
}
